<template>
  <base-breadcrumb>
    <searchData :search-data="searchArr" @search="search" />
    <a-card :bordered="false">
      <!-- <div class="table-operator">
        <div class="pub-table-action-btn">
          <a-button
            v-btnPermission="'mm_006_add_01'"
            type="primary"
            icon="plus"
            @click="handleEdit(null,1)"
          >
            新增
          </a-button>

          <excel
            ref="exportSelect"
            file-name="物资招标文件评审"
            is-click-generate="false"
          >
            <a-button
              slot="excelBtn"
              @click="exportSelect"
            >
              导出选中
            </a-button>
          </excel>
          <excel
            ref="exportAll"
            file-name="物资招标文件评审"
            is-click-generate="false"
          >
            <a-button
              slot="excelBtn"
              @click="exportAll"
            >
              导出全部
            </a-button>
          </excel>
        </div>
        <div class="pub-table-select-count">
          <a-icon
            type="info-circle"
            theme="twoTone"
          />
          已选择{{ selectedRowKeys.length }}项
          <span style="padding-left: 10px;">
            总计：{{ totalSize }}，
            待提交审核：{{ statistics.draft }}，
            审核中：{{ statistics.submitted }}，
            审核通过：{{ statistics.past }}，
            审核不通过：{{ statistics.rejected }}，
            已作废：{{ statistics.delete }}
          </span>
        </div>
      </div> -->
      <list-table
        ref="table"
        :pageKeys="['pageNum', 'pageSize', 'total']"
        :columns="columns"
        :startIndex="0"
        :request="loadData"
        :exportPermission="{ select: '', all: '' }"
        :exportConfig="{ use: true, select: $route.meta.title, all: $route.meta.title }"
        :statistics-config="{ use: true, statistics }"
      >
        <template slot="prefix" slot-scope="ref">
          <a-button v-btnPermission="'mm_006_add_01'" type="primary" icon="plus" @click="handleEdit(null, 1)"> 新增 </a-button>
        </template>
        <span slot="index" slot-scope="text, record, index">{{ index + 1 }}</span>
        <template slot="auditStatusStr" slot-scope="text, record, index">
          <list-status :status="record.auditStatus" :text="text" />
        </template>
        <template slot="operation" slot-scope="text, record, index">
          <div class="pub-table-list-action">
            <span @click="handleEdit(record, 3)">查看</span>
            <span v-if="record.auditStatus === 'PAST'" @click="invalidResult(record)">作废</span>
            <template v-if="record.auditStatus === 'DRAFT' || record.auditStatus === 'REJECTED'">
              <span @click="() => handleEdit(record, 2)">编辑</span>
              <span @click="() => handleDel(record.id)">删除</span>
            </template>
            <approval-button :id="record.id" :status="record.auditStatus"
                             v-if="record.auditStatus === 'PAST' || record.auditStatus === 'SUBMITTED' || record.auditStatus === 'REJECTED' || record.auditStatus === 'REVOKEIN' || record.auditStatus === 'REVOKE' || record.auditStatus === 'ABOLISH' || record.auditStatus === '1' || record.auditStatus === '3' || record.auditStatus === 'COMMIT'|| record.auditStatus === '2' "
            />
          </div>
        </template>
      </list-table>
      <!-- <pagination
        v-model="pageNo"
        :total="totalSize"
        :page-size.sync="pageSize"
        @change="handlePageChange"
        @showSizeChange="showSizeChange"
      /> -->
    </a-card>
  </base-breadcrumb>
</template>
<script>
import { reviewList, reviewListDel, invalid, findListCount } from '@/api/material/procurement/review'
import { selectOption } from '@/api/material/goods/goods'
import SelectSubCompany from '@/components/Select/SelectSubCompany'
import SelectProject from '@/components/Select/SelectProject'

export default {
  name: 'a' + Date.now(),
  data: function () {
    return {
      statistics: {},
      loading: false,
      pageNo: 1,
      queryParam: {},
      pageSize: 10,
      totalSize: 0,
      searchArr: [
        // 搜索框
        {
          name: '所属分公司',
          valueKey: 'branchCompanyId',
          type: SelectSubCompany
        },
        // {
        //   name: '项目名称',
        //   type: SelectProject,
        //   valueKey: 'projectId'
        // },
        {
          name: '招标文件评审编码',
          value: 'fileCode',
          type: 'input'
        },
        {
          name: '招标采购申请编码',
          value: 'purchaseApplyCode',
          type: 'input'
        },
        {
          name: '工程名称',
          value: 'wbsId',
          type: 'select',
          data: [],
          request: () => selectOption({ type: 18 })
        },
        {
          name: '招标文件编号',
          value: 'fileNumber',
          type: 'input'
        },
        {
          name: '招标类别',
          value: 'tenderTypeId',
          type: 'select',
          data: [],
          request: () => selectOption({ type: 5 })
        },
        {
          name: '创建人',
          value: 'creatorName',
          type: 'input'
        },
        {
          name: '创建时间',
          value: 'createTime',
          type: 'timeRangeFormat'
        },
        {
          name: '状态',
          value: 'auditStatus',
          type: 'select',
          data: this.STATUS
        }
      ],
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          scopedSlots: { customRender: 'index' },
          width: 70
        },
        {
          title: '所属分公司',
          dataIndex: 'branchCompanyName',
          width: 150
        },
        {
          title: '项目名称',
          dataIndex: 'projectNameByAuth',
          width: 150
        },
        {
          title: '物资招标文件评审编码',
          dataIndex: 'fileCode',
          width: 180
        },
        {
          title: '物资招标采购申请编码',
          dataIndex: 'purchaseApplyCode',
          width: 180
        },
        {
          title: '工程名称',
          dataIndex: 'projectName',
          width: 160
        },
        {
          title: '招标文件编号',
          dataIndex: 'fileNumber',
          width: 160
        },
        {
          title: '招标类别',
          dataIndex: 'tenderTypeName',
          width: 160
        },
        {
          title: '创建人',
          dataIndex: 'creatorName',
          width: 160
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          width: 160
        },
        {
          title: '状态',
          dataIndex: 'auditStatusStr',
          width: this.STATUS_WIDTH,
          scopedSlots: { customRender: 'auditStatusStr' }
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' },
          width: 180,
          fixed: 'right'
        }
      ],
      selectedRowKeys: [],
      selectId: [],
      selectedRows: [],
      chooseData: 0,
      tableData: [],
      pathName: this.$route.path,
      loadData: (params) => {
        let searchData = Object.assign(
          {
            pageSize: this.pageSize || this.totalSize,
            pageNo: this.pageNo
          },
          params,
          this.queryParam
        )
        findListCount(searchData).then((res) => {
          if (res.code === this.SUCCESS_CODE) this.statistics = res.data
        })
        return reviewList(searchData).then((res) => {
          return res
        })
      },
      temp: () => {
        return (
          <span>
            总计：{this.totalSize || 0}， 待提交审核：{this.statistics.draft || 0}， 审核中： {this.statistics.submitted || 0}， 审核通过：{this.statistics.past || 0}， 审核不通过： {this.statistics.rejected || 0},
            已作废： {this.statistics.delete || 0}, 已变更： {this.statistics.change || 0},
          </span>
        )
      }
    }
  },
  created() {
    // this.list()
    this.$refs.table.refresh(true)
  },
  mounted: function () {
    this.$refs.table.refresh(true)
    // this.list()
    // this.$refs.exportSelect.tableToExcelFile(this.$refs.table)
    // this.$refs.exportAll.tableToExcelFile(this.$refs.table)
    // console.log('mounted-----------table的Path', this.pathName)
    // 监听详情页返回刷新列表
    //this.pathName取的值是this.$route.path
    this.$bus.$on('handleRefresh' + this.pathName, () => {
      this.$refs.table.refresh(true)
      // console.log('--------------------收到了，刷新刷新刷新----------------', this.pathName)
      // this.list()
    })
  },
  beforeDestroy() {
    console.log('beforeDestroy-------table的Path', this.pathName)
    this.$bus.$off('handleRefresh' + this.pathName)
  },
  methods: {
    // exportSelect: function () {
    //   if (this.selectedRowKeys.length > 0) {
    //     this.$refs.exportSelect.setDataAndGenerate(this.selectedRows)
    //   } else {
    //     this.$message.warning('请选择需要导出的数据')
    //   }
    // },
    // exportAll: function () {
    //   // 参数：查询条件、导出的回调
    //   if (this.totalSize) {
    //     this.pageNo = 1
    //     this.list(false, this.$refs.exportAll.setDataAndGenerate)
    //   } else {
    //     this.$message.warning('没有可导出的数据')
    //   }
    // },
    invalidResult: function (row) {
      this.invalidList(invalid, this.search, { id: row.id })
    },
    list(params, callback) {
      let _this = this
      let searchData = Object.assign(
        {
          pageSize: callback ? _this.totalSize : _this.pageSize,
          pageNo: _this.pageNo
        },
        params,
        this.queryParam
      )
      // if (searchData.createTime) {
      //     searchData.createTimeStart = this.formatDate(searchData.createTime[0])
      //     searchData.createTimeEnd = this.formatDate(searchData.createTime[1])
      // }
      delete searchData.createTime
      this.loading = true
      findListCount(searchData).then((res) => {
        if (res.code === this.SUCCESS_CODE) this.statistics = res.data
      })
      reviewList(searchData).then((res) => {
        if (res.code === 200) {
          if (callback) {
            if (res.data.length) {
              callback(res.data)
            } else {
              this.$message.warning('没有可导出的数据')
            }
            this.loading = false
            return
          }
          _this.tableData = res.data
          _this.totalSize = res.totalSize
        }
        this.loading = false
      })
    },
    search(val) {
      this.queryParam = val
      this.pageNo = 1
      // this.list(val)
      this.$refs.table.refresh(true)
    },
    // handlePageChange(pageNo, pageSize) {
    //   this.pageNo = pageNo
    //   this.list()
    //   this.selectedRowKeys = []
    // },
    // showSizeChange(pageNo, pageSize) {
    //   this.pageNo = 1
    //   this.pageSize = pageSize
    //   this.list()
    // },
    // onSelectChange(selectedRowKeys, selectedRows) {
    //   let _this = this
    //   _this.selectId = []
    //   selectedRows.map((item) => {
    //     // 选中id
    //     _this.selectId.push(item.id)
    //   })
    //   this.chooseData = selectedRowKeys.length
    //   _this.selectedRowKeys = selectedRowKeys
    //   _this.selectedRows = selectedRows
    // },
    handleDel(id) {
      let _this = this
      let params = id ? [id] : _this.selectId
      _this.delList(
        reviewListDel,
        () => {
          _this.search()
          this.selectedId = []
          this.selectedRowKeys = []
          this.selectedRows = []
          this.editingKey = ''
        },
        { ids: params }
      )
    },
    handleEdit(record, state) {
      this.$router.push({
        path: '/material/procurement/reviewAdd',
        query: {
          id: (record && record.id) || '',
          state: state
        }
      })
    }
  }
}
</script>
